package net.htjs.juc.multilock;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MultiLockDemo {

    public static void main(String[] args) {
        BigRoom bigRoom = new BigRoom();
        new Thread(() -> {
            bigRoom.sleep();
        }, "t1").start();

        new Thread(() -> {
            bigRoom.study();
        }, "t2").start();
    }

    @Slf4j
    static class BigRoom {
        private final Object  sleepRoom = new Object();
        private final Object studyRoom = new Object();
        public void sleep() {
            synchronized (sleepRoom) {
                log.debug("sleep...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public void study() {
            synchronized (studyRoom) {
                log.debug("study...");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
